package wqh.modbus;

import cn.tpson.sewercover.core.ISewerCoverDataCallBack;
import cn.tpson.sewercover.core.SCAlarm;
import cn.tpson.sewercover.core.TempAlarm;
import cn.tpson.sewercover.core.SewerCoverManager;

import cn.tpson.sewercover.core.TempAlarm;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.web.socket.TextMessage;
import wqh.controller.api.GlobalData;
import wqh.domain.*;
import wqh.dao.*;
import wqh.global.GlobalChannel;
import wqh.global.GlobalChannelData;
import wqh.global.GlobalDevice;
import wqh.pojo.IPConver;
import wqh.pojo.SelectMonitorCondition;
import wqh.service.*;

import wqh.websocket.device_websocket.DeviceSocketHandler;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * Created by cfs on 2016/12/23.
 */
public class DtDataCallBack implements ISewerCoverDataCallBack{

    @Autowired
    DeviceSocketHandler deviceSocketHandler;

    @Autowired
    private DtMonitorService dtMonitorService;
    @Autowired
    private DtGeoBuildingService dtGeoBuildingService;

    @Autowired
    private DtAlarmEventService dtAlarmEventService;

    @Autowired
    private DtMapPosService dtMapPosService;

    @Autowired
    private DtDeviceService  dtDeviceService;


    public void recieve4SC(String s, SCAlarm d)
    {
        System.out.println("动态监测角度值");
        String makrId = d.getMarkID();
        int alarm = d.getSlope();
        DtDevice dtDevice = dtDeviceService.findAllByIP(s);
        if(dtDevice != null){
            dtDevice.setLast_time((int)(System.currentTimeMillis()/1000));
            dtDeviceService.updateById(dtDevice);
            SelectMonitorCondition selectMonitorCondition = new SelectMonitorCondition(s,makrId);
            DtMonitor dtMonitor = dtMonitorService.findByIPAndMarkID(selectMonitorCondition);
            if(dtMonitor != null && (dtMonitor.getAlarm_value() < alarm))
            {
                DtAlarmEvent dtAlarmEvent = new  DtAlarmEvent();
                dtAlarmEvent.setUnix_time((int)(System.currentTimeMillis()/1000));
                String area = "无地理信息";
                String building = "无地理信息";
                String floor = "无地理信息";
                if(dtMonitor.getGeo_id() != 0){
                    DtGeoBuilding gA = dtGeoBuildingService.findRecordById(dtMonitor.getGeo_id());
                    if(gA != null){
                        if(gA.getLevel() == 1){
                            area = gA.getName();
                        }else if(gA.getLevel() == 2){
                            area = gA.getName();
                            DtGeoBuilding gB = dtGeoBuildingService.findRecordById(gA.getParent_id());
                            building = gB.getName();
                        }else if(gA.getLevel() == 3){
                            area = gA.getName();
                            DtGeoBuilding gB = dtGeoBuildingService.findRecordById(gA.getParent_id());
                            building = gB.getName();
                            DtGeoBuilding gC = dtGeoBuildingService.findRecordById(gB.getParent_id());
                            floor = gC.getName();
                        }else {
                            area = "地理信息异常";
                            building = "地理信息异常";
                            floor = "地理信息异常";
                        }
                    }
                }
                dtAlarmEvent.setArea(area);
                dtAlarmEvent.setBuilding(building);
                dtAlarmEvent.setFloor(floor);
                dtAlarmEvent.setDt_device_ip(dtDevice.getIp());
                dtAlarmEvent.setDt_device_name(dtDevice.getName());
                dtAlarmEvent.setDt_monitor_name(dtMonitor.getMonitor_name());
                dtAlarmEvent.setDt_alarm_data(alarm);
                dtAlarmEvent.setUnit("°(度)");
                try{
                    dtAlarmEventService.insertRecord(dtAlarmEvent);
                }catch (Exception e){
                    e.printStackTrace();
                    System.out.println(e);
                }
            }
        }

    }
    public void recieve4Temp(String s, TempAlarm b)
    {
        System.out.println("动态监测温度值");
    }
    public void heart(String s,SCAlarm d)
    {
        System.out.println("动态监测heart");
        System.out.println(s);
        System.out.println("########################################");
        System.out.println(d);
        System.out.println("########################################");
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String TimeString = time.format(new java.util.Date());
        System.out.println(TimeString);
    }

    public void addDtAlarmEvent()
    {
        DtAlarmEvent a = new DtAlarmEvent();
        a.setUnix_time((int)(System.currentTimeMillis()/1000));

    }
}
